package de.hwrberlin.expertsystem.util;

import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;

import de.hwrberlin.remo.model.ElementState;
import de.hwrberlin.remo.model.KnowledgeBase;
import de.hwrberlin.remo.model.KnowledgeBaseElement;
import de.hwrberlin.remo.model.LinkObject;
import de.hwrberlin.remo.model.RemoModelPackage;
import de.hwrberlin.remo.model.RemoObject;
import de.hwrberlin.remo.model.Rule;

public class ModelEditingHelper {
	
	private EditingDomain domain;
	
	private final EAttribute OBJECT_NAME        = RemoModelPackage.eINSTANCE.getRemoObject_Name();
	private final EAttribute OBJECT_DESCRIPTION = RemoModelPackage.eINSTANCE.getRemoObject_Description();
	private final EAttribute ELEMENT_STATE      = RemoModelPackage.eINSTANCE.getKnowledgeBaseElement_State();
	private final EAttribute RULE_VALIDATED     = RemoModelPackage.eINSTANCE.getRule_Validated();
	private final EAttribute LINKOBJECT_NAME    = RemoModelPackage.eINSTANCE.getLinkObject_Linkname();
	private final EAttribute LINK_STATE         = RemoModelPackage.eINSTANCE.getLinkObject_Linkstate();
	
	private final EReference KNOWLEDGEBASE_ELEMENTS = RemoModelPackage.eINSTANCE.getKnowledgeBase_OwnedElements();
	
	public ModelEditingHelper(EditingDomain domain) {
		this.domain = domain;
	}
	
	public void setObjectName(RemoObject object, String newName) {
		Command command = SetCommand.create(domain, object, OBJECT_NAME, newName);
		domain.getCommandStack().execute(command);
	}
	
	public void setObjectDescription(RemoObject object, String newDescription) {
		Command command = SetCommand.create(domain, object, OBJECT_DESCRIPTION, newDescription);
		domain.getCommandStack().execute(command);
	}
	
	public void setElementState(KnowledgeBaseElement element, ElementState newState) {
		Command command = SetCommand.create(domain, element, ELEMENT_STATE, newState);
		domain.getCommandStack().execute(command);
	}
	
	public void setObjectName(LinkObject object, String newName) {
		Command command = SetCommand.create(domain, object, LINKOBJECT_NAME, newName);
		domain.getCommandStack().execute(command);
	}
	
	public void setElementState(LinkObject element, ElementState newState) {
		Command command = SetCommand.create(domain, element, LINK_STATE, newState);
		domain.getCommandStack().execute(command);
	}
	
	public void setRuleValidated(Rule rule, boolean wasValidated) {
		Command command = SetCommand.create(domain, rule, RULE_VALIDATED, wasValidated);
		domain.getCommandStack().execute(command);
	}
	
	public void setElements(KnowledgeBase base, List<KnowledgeBaseElement> elements) {
		Command command = SetCommand.create(domain, base, KNOWLEDGEBASE_ELEMENTS, elements);
		domain.getCommandStack().execute(command);
	}
	
	public void resetRules(KnowledgeBase expertSystem) {
		for(Rule rule : expertSystem.getRules()) {
			setElementState(rule, ElementState.UNKNOWN);
			setRuleValidated(rule, false);
		}
	}
	
}